package com.cq.action;

import java.io.File;
import java.lang.reflect.Constructor;

import com.amarsoft.are.ARE;
import com.cq.action.AbstractRptParse;
import com.cq.util.StringUtil;

public class BeanToDB
{
    private String filePath;
    private String urlPath;
    private String fileName;
    private AbstractRptParse rptHandler;

    public BeanToDB(String filePath, String urlPath, String fileName)
    {
        this.filePath = filePath;
        this.urlPath = urlPath;
        this.fileName = fileName;
    }

    public static void main(String[] args)
    {
        ARE.init("etc/are.xml");//初始化ARE
        BeanToDB bd = new BeanToDB("etc/1515462502498.txt","http://localhost:8080/firefly","firefly");
        try
        {
            bd.beanToDB();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 调用creditRptHandler类
     * */
    public void beanToDB() throws Exception
    {
        try
        {
            setRptHandler();//初始化rptHandler
            this.rptHandler.parse();//实际调用的是CreditRptHandler.parse();
        } catch (Exception e)
        {
            throw e;
        }
    }

    /**
     * 初始化CreditRptHandeler类
     * 
     * @throws Exception
     */
    private void setRptHandler() throws Exception
    {
        try
        {
            String rptHandler = StringUtil.trim(ARE.getProperty("reportHandler"));
            if (rptHandler == null)
            {
                throw new Exception("ARE环境变量reportHandler未配置");
            }
            Class<?>[] argtype = new Class[]
            {String.class,String.class,String.class};
            Object[] argparam = new Object[]
            {StringUtil.readFile(new File(filePath)),this.urlPath,this.fileName};
            Class<?> classType = Class.forName(rptHandler);
            Constructor<?> constructor = classType.getDeclaredConstructor(argtype);
            this.rptHandler = (AbstractRptParse) constructor.newInstance(argparam);
        } catch (Exception e)
        {
            throw new Exception("设置报表解析器出错", e);
        }
    }
}
